package com.leetcode2;

public class Index838pushDominoes {

	public static void main(String[] args) {
		String dominoes = "RR.L";
		Index838pushDominoes pD = new Index838pushDominoes();
		System.out.println(pD.pushDominoes(dominoes));
	}
	
	public String pushDominoes(String dominoes) {
		char[] strs = dominoes.toCharArray();
		int i = 0,temp = 0,j;
		for (i = 0; i < strs.length; i++) {
			if(strs[i] == 'R') {
				for(j = i+1;j < strs.length;j ++) {
					if(strs[j] == 'R') {
						changetoR(strs,i,j);//
						i = j;
						temp = j+1;
						continue;
					}
					if(strs[j] == 'L') { 
						changetoRL(strs,i,j);
						i = j;
						temp = j+1;
						break;
					}
				}
				if(j == strs.length) changetoR(strs,i,strs.length-1);
			}
			else if(strs[i] == 'L') {
				changetoL(strs,temp,i);
				temp = i+1;
			}
		}	
		return new String(strs);
    }
	
	public void changetoRL(char[] strs,int low,int high) {
		while(low < high) {
			strs[low++] = 'R';
			strs[high--] = 'L';
		}
	}
	
	public void changetoL(char[] strs,int low,int high) {
		while(low <= high) strs[low++] = 'L';
	}
	
	public void changetoR(char[] strs,int low,int high) {
		while(low <= high) strs[low++] = 'R';
	}
}
